package com.xbongbong.pro.statistics.help;


import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.XbbConstant;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.domain.entity.TableColumsEntity;
import com.xbongbong.paas.enums.bi.XbbTableRefEnum;
import com.xbongbong.saas.domain.dao.TableColumsDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 描述信息
 *
 * @ClassName JDBCEtl
 * @Author 梁鲁江
 * 创建时间 2021/7/27 19:42
 * 修改时间  2021/7/27 19:42
 **/
@Component
public class JdbcEtl {
    @Resource
    private TableColumsDao tableColumsDao;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    private final Logger LOG = LoggerFactory.getLogger(getClass());
    public Map<String, Set<String>> process() {
        Map<String,Set<String>> outMap  = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        try {
            Set<String> tableNameSet = XbbTableRefEnum.getTableNames();
            List<TableColumsEntity> list = tableColumsDao.findColumns(tableNameSet);
            if(CollectionsUtil.isNotEmpty(list)){
                for (TableColumsEntity tableColumsEntity : list) {
                    String tableName = tableColumsEntity.getTableName();
                    String field = tableColumsEntity.getField();
                    Set<String> tableColumsSet;
                    if(outMap.containsKey(tableName)){
                        tableColumsSet = (Set<String>) outMap.get(tableName);
                        if(tableColumsSet == null){
                            tableColumsSet = new HashSet();
                        }
                    }else {
                        tableColumsSet = new HashSet();
                    }
                    tableColumsSet.add(field);
                    outMap.put(tableName,tableColumsSet);
                }
            }
            paasRedisHelper.setValue(XbbConstant.FILEPREFIX, XbbConstant.TABLE_COLUMS_KEY,outMap,XbbConstant.ONE_DAY);
            return outMap;
        } catch (Exception e) {
            LOG.info("",e);
        }
        return outMap;
    }
}
